/*
 * gstengine.h - Gstreamer sound engine
 *
 * Copyright (C) 2010-2011 Serge Begger
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef GSTENGINE_H
#define GSTENGINE_H

#include <QThread>
#include <QApplication>
#include <qwindowdefs.h>
#include <QSettings>
#include <QX11Info>
#include <QFrame>
#include <gst/gst.h>
#include <gst/interfaces/tuner.h>
#include <gst/interfaces/propertyprobe.h>
#include <gst/interfaces/xoverlay.h>

#include "videowidget.h"

class gstengine : public QObject
{
    Q_OBJECT
public:
    explicit gstengine(QObject *parent = 0);
    ~gstengine();
    void mute(bool);
    void setVolume(int);
    int getVolume();
    void setFreq(double *band);
    bool loopaudio();
    bool initEngine;
    int currVolume;
    void stopPipeline();
    bool getStatePipeline();
    //tv
    void setanalogTvCh(double freq);
    int getStrenght();
    bool analogTv(QFrame *frame);
    void setWinId(WId xwinid);
    //dvb
    bool initDVB(QFrame *frame, QStringList paramList);
    //player
    bool initPlayer(QFrame *frame,QString filename);
    bool playFile();
    void seekPlayer(gint64 time);
    int getPlayerDuration();
    qint64 getPlayerPosition();
    bool isPlayerPlayed();
    void pausePlayer();
    void stopPlayer();
    bool isVideo();
    QRect currGeometry;

    bool error;
    QString ermessage;
    void setVideoLCDtext(QString info);
    void setGeometryWV(QRect geometry);

signals:
    void toFullScreen();
    void playBinStChanged();

private:
    GstElement *loopPipeline, *pipeline, *input, *volumeCtrl;
    GstElement *equalizer;
    GstElement *textoverlay;
    GstElement *videoSink;
    GstElement *audioSink;
    //player
    GstElement *playbin;
    void fileType(QString filename);
    //tv
    GstElement *source;
    GstBus *bus;
    GstTunerChannel *tunerCh;
    GstTuner *tuner;
    //DVB
    GstElement *dvbsrc;
    QString typeCh;

    QSettings *settings;

    QString inputDevName;
    QString outputDevName;
    QString tvDevName;

    bool createAudioSink();
    bool createVideoSink();
    bool createVideoSinkPlayBin();
    bool createVolumeCtrl();

    QX11Info *xinfo;

    videoWidget *widgetVideo;
    bool fullScreen;
    QWidget *videoFrame;
    void initVideo();

protected:

public slots:
    void clickVideoWidget();
    void clearVideoLCDtext();
};

struct devInfo{
    int type;
    QString name;
    QString path;
};

class getGstDevices : public QThread
{
    Q_OBJECT

public:
    explicit getGstDevices(QObject *parent = 0);
    QList<devInfo> deviceList;
    bool error;
    QString ermessage;

protected:
    void run();

private:
    void getDevice(QString,int);

};

#endif // GSTENGINE_H
